package com.hxzy.order.service.impl;

import cn.hxzy.common.util.RandomId;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hxzy.order.entity.TlLoaderUnloadLog;
import com.hxzy.order.entity.TlWaybillLog;
import com.hxzy.order.entity.TtWaybill;
import com.hxzy.order.mapper.TlLoaderUnloadLogMapper;
import com.hxzy.order.mapper.TlWaybillLogMapper;
import com.hxzy.order.mapper.TtWaybillMapper;
import com.hxzy.order.service.TlLoaderUnloadLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxzy.order.service.TlWaybillLogService;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.enums.DataSourceType;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mingqian.Wu
 * @since 2023-06-27
 */
@Service
@DataSource(DataSourceType.ORDER)
public class TlLoaderUnloadLogServiceImpl extends ServiceImpl<TlLoaderUnloadLogMapper, TlLoaderUnloadLog> implements TlLoaderUnloadLogService {

    @Autowired
    private TlWaybillLogMapper tlWaybillLogMapper;

    @Autowired
    private TtWaybillMapper ttWaybillMapper;


    @Override
    public List<TlLoaderUnloadLog> getLoaderUnloagList(String id) {
        QueryWrapper<TlLoaderUnloadLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("waybill_id", id);
        List<TlLoaderUnloadLog> tlLoaderUnloadLogs = baseMapper.selectList(queryWrapper);
        return tlLoaderUnloadLogs;
    }

    @Override
    @Transactional
    public boolean addUnloadLog(TlLoaderUnloadLog tlLoaderUnloadLog) {
        TlWaybillLog tlWaybillLog = new TlWaybillLog();
        tlWaybillLog.setEntryTime(tlLoaderUnloadLog.getEntryTime());
        tlWaybillLog.setUnloadingTime(new Date());
        tlWaybillLog.setWaybillId(tlLoaderUnloadLog.getWaybillId());
        UpdateWrapper<TlWaybillLog> updateWrapper = new UpdateWrapper<>();


        updateWrapper.eq("waybill_id", tlLoaderUnloadLog.getWaybillId());
        int update = tlWaybillLogMapper.update(tlWaybillLog, updateWrapper);
        UpdateWrapper<TtWaybill> updateWrapper1 = new UpdateWrapper<>();
        updateWrapper1.eq("waybill_id", tlLoaderUnloadLog.getWaybillId());
        updateWrapper.set("waybill_status", 106);
        TtWaybill ttWaybill = new TtWaybill();
        ttWaybill.setWaybillStatus(106);
        int update1 = ttWaybillMapper.update(ttWaybill, updateWrapper1);

        if (update1 < 1) {
            return false;
        }
        if (update < 1) {
            return false;
        }
        tlLoaderUnloadLog.setId(RandomId.getUnloadId());
        int insert = baseMapper.insert(tlLoaderUnloadLog);
        if (insert < 1) {
            return false;
        }
        return true;
    }
}
